<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Models\UserAsset;
use App\Models\UserWithdraw;
use App\Models\UserDepositMethod;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;

class WithdrawController extends ApiBaseController
{
    public function withdraw(Request $request, UserWithdraw $model)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'address' => 'bail|required', //提现地址
            'from_amount' => 'bail|required|numeric', //金额
            'pay_pwd' => 'bail|required|numeric', //密码
        ])) return $vr;

        $data['uid'] = $this->getUid($request);
        $result = $model->withdraw($data);
        return $this->success($result);
    }


    public function history(Request $request, UserWithdraw $model)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'page' => 'required|integer', //充值订单
        ])) return $vr;
        $data['uid'] = $this->getUid($request);
        return $this->success($model->history($data));
    }

    public function method(Request $request)
    {
        return $this->success(UserDepositMethod::methodList());
    }


    public function prepare(Request $request)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
        ])) return $vr;

        $data['uid'] = $this->getUid($request);
        $result['min'] = config("withdraw.min");
        $result['max'] = config("withdraw.max");
        $result['rate'] =config("withdraw.rate");

        /**
         * @var $user User
         */
        $user = User::getOne($data['uid']);

        $result['is_pay_pwd_set'] = (bool)$user->pay_password;
        $user_asset = UserAsset::getContractAccount($data['uid']);
        $result['balance'] = asMoney($user_asset->balance);

        return $this->success($result);
    }
}
